package com.originskyseed.platform.task.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.task.mapper.SpreadLimitMapper;
import com.originskyseed.platform.task.mapper.TaskRelateAssignPersonMapper;
import com.originskyseed.platform.task.model.SpreadLimit;
import com.originskyseed.platform.task.model.TaskRelateAssignPerson;
import com.originskyseed.platform.task.service.ITaskRelateAssignPersonService;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 任务关联指派人表Service接口实现类（OSI_TASK_RELATE_ASSIGN_PERSON）
 *
 * @author wlj
 */
@DubboService
@Component("taskRelateAssignPersonService")
public class TaskRelateAssignPersonServiceImpl implements ITaskRelateAssignPersonService {

	@Resource
	private TaskRelateAssignPersonMapper taskRelateAssignPersonMapper;

	@Resource
	private SpreadLimitMapper spreadLimitMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.taskRelateAssignPersonMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.taskRelateAssignPersonMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(TaskRelateAssignPerson record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.taskRelateAssignPersonMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(TaskRelateAssignPerson record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.taskRelateAssignPersonMapper.insertSelective(record);
	}

	/**
	 * 批量插入
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertBatch(List<TaskRelateAssignPerson> list, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.insertBatch(list);
	}

	/**
	 * 批量新增
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertBatch(List<TaskRelateAssignPerson> list) throws Exception {
		return this.taskRelateAssignPersonMapper.insertBatch(list);
	}

	/**
	 * 抢单任务处理
	 *
	 * @param taskId
	 * @param anchorId
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertTaskSelective(String taskId, String anchorId, UserInfo userInfo) throws Exception {
		// 自主新增一条为系统默认
		SpreadLimit limit = new SpreadLimit();
		if (userInfo.getSourceSys() == 1) {
			limit.setObjId("-1");
			limit.setObjId("-1");
		} else {
			limit.setObjId("-2");
			limit.setObjId("-2");
		}
		SpreadLimit spreadLimit1 = this.spreadLimitMapper.selectBySpreadLimit(limit);
		if (spreadLimit1 != null) {
			SpreadLimit limitInsert = new SpreadLimit();
			limitInsert.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
			limitInsert.setCreateTime(new Date());
			limitInsert.setCreatorId(userInfo.getId());
			limitInsert.setCreator(userInfo.getName());
			limitInsert.setObjId(userInfo.getId());
			limitInsert.setObjName(userInfo.getName());
			limitInsert.setNum(spreadLimit1.getNum());
			limitInsert.setRemainNum(spreadLimit1.getNum());
			limitInsert.setState(1);
			this.spreadLimitMapper.insertSelective(limitInsert);

			TaskRelateAssignPerson taskRelateAssignPerson = new TaskRelateAssignPerson();
			taskRelateAssignPerson.setUserId(userInfo.getId());
			taskRelateAssignPerson.setName(userInfo.getName());
			taskRelateAssignPerson.setTaskId(taskId);
			taskRelateAssignPerson.setAccount(userInfo.getAccount());
			taskRelateAssignPerson.setType(2);// 抢单
			taskRelateAssignPerson.setState(0);
			taskRelateAssignPerson.setUserType(2);
			taskRelateAssignPerson.setCreateTime(new Date());
			this.insertSelective(taskRelateAssignPerson);
			// 该业务员接单上限统计-1
			this.spreadLimitMapper.updateSubtractById(userInfo.getId());
		}
		return 1;
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(TaskRelateAssignPerson record) throws Exception {
		record.setUpdateTime(new Date());
		return this.taskRelateAssignPersonMapper.updateByPrimaryKey(record);
	}

	/**
	 * 通过任务ID更新任务状态逾期
	 *
	 * @param taskId
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateStateByTaskId(String taskId, int state, String userId) throws Exception {
		return this.taskRelateAssignPersonMapper.updateStateByTaskId(taskId, state, userId);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(TaskRelateAssignPerson record) throws Exception {
		record.setUpdateTime(new Date());
		return this.taskRelateAssignPersonMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 更新记录（租户模式）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelectiveTenantCode(TaskRelateAssignPerson record, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.updateByPrimaryKeySelective(record);
	}

	/**
	 * 推广任务完成
	 * @param taskId
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateFinishTaskByTaskId(String taskId) throws Exception {
		return this.taskRelateAssignPersonMapper.updateFinishTaskByTaskId(taskId);
	}

	@Override
	public int updateFinishTaskByTaskIdTenantCode(String taskId, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.updateFinishTaskByTaskId(taskId);
	}

	/**
	 * 任务已读
	 *
	 * @param taskId
	 * @return
	 * @throws Exception
	 */
	@Override
	public int taskIsRead(String taskId, String userId) throws Exception {
		return this.taskRelateAssignPersonMapper.taskIsRead(taskId,userId);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public TaskRelateAssignPerson selectByPrimaryKey(String id) throws Exception {
		return this.taskRelateAssignPersonMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<TaskRelateAssignPerson> selectList(TaskRelateAssignPerson record) throws Exception {
		return this.taskRelateAssignPersonMapper.selectList(record);
	}

	/**
	 * 定时任务查询（租户模式）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<TaskRelateAssignPerson> selectList(TaskRelateAssignPerson record, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<TaskRelateAssignPerson> selectListByPage(Map<String, Object> params, BasePage<TaskRelateAssignPerson> page) throws Exception {
		params.put("page", page);
		List<TaskRelateAssignPerson> list = this.taskRelateAssignPersonMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 根据taskId更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByTaskId(TaskRelateAssignPerson record) throws Exception {
		return this.taskRelateAssignPersonMapper.updateByTaskId(record);
	}

	/**
	 * 通过任务id删除任务
	 *
	 * @param taskId
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByTaskId(String taskId) throws Exception {
		return this.taskRelateAssignPersonMapper.deleteByTaskId(taskId);
	}

	/**
	 * 查询是否有我数据
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public TaskRelateAssignPerson selectIsMyTask(Map<String, Object> map) throws Exception {
		return this.taskRelateAssignPersonMapper.selectIsMyTask(map);
	}

	/**
	 * 根据任务ID查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public TaskRelateAssignPerson selectByTaskId(String id) throws Exception {
		return this.taskRelateAssignPersonMapper.selectByTaskId(id);
	}

	/**
	 * 根据任务ID查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<TaskRelateAssignPerson> selectListByTaskId(String id) throws Exception {
		return this.taskRelateAssignPersonMapper.selectListByTaskId(id);
	}

	@Override
	public List<TaskRelateAssignPerson> selectListByTaskIdTenantCode(String id, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.selectListByTaskId(id);
	}

	/**
	 * 根据对象查询
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public TaskRelateAssignPerson selectByTaskTaskRelateAssignPerson(TaskRelateAssignPerson record) throws Exception {
		return this.taskRelateAssignPersonMapper.selectByTaskTaskRelateAssignPerson(record);
	}

	/**
	 * 考卷查询是否有我记录
	 *
	 * @param taskId
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@Override
	public TaskRelateAssignPerson selectPaperIsMyTask(String taskId, String userId) throws Exception {
		return this.taskRelateAssignPersonMapper.selectPaperIsMyTask(taskId, userId);
	}

	/**
	 * 查询是否有我记录
	 *
	 * @param taskId
	 * @param userId
	 * @param tenantCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public TaskRelateAssignPerson selectPaperIsMyTaskTenantCode(String taskId, String userId, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.selectPaperIsMyTask(taskId, userId);
	}

	/**
	 * 统计未读
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@Override
	public Integer countTaskIsRead(String userId) throws Exception {
		return this.taskRelateAssignPersonMapper.countTaskIsRead(userId);
	}

}
